package co.fitstart.admin.web.controller.market;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.market.MarketWechatImage;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("wechat-images")
@MenuSetting("menu-market")
public class MarketWechatImageController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<MarketWechatImage> marketWechatImageService;
    
    private static final Logger logger = LoggerFactory.getLogger(MarketWechatImageController.class);
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-market-wechat")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("market/market-wechat-image/wechat-image-list");
        
        List<MarketWechatImage> marketWechatImages = marketWechatImageService.listAll(MarketWechatImage.class);
        modelAndView.addObject("marketWechatImages", marketWechatImages);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param wechatImageId
     * @return
     */
    @RequestMapping(value = "/{wechatImageId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("wechatImageId") Long wechatImageId) {
        ModelAndView modelAndView = new ModelAndView("market/market-wechat-image/wechat-image-edit");
        
        MarketWechatImage marketWechatImage = marketWechatImageService.load(MarketWechatImage.class, wechatImageId);
        modelAndView.addObject("marketWechatImage", marketWechatImage);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param wechatImageId
     * @param cover
     * @return
     */
    @RequestMapping(value = "/{wechatImageId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@PathVariable("wechatImageId") Long wechatImageId,
                                       @RequestParam("cover") String cover) {
        Result result = new Result();
        
        try {
            
            MarketWechatImage marketWechatImage = marketWechatImageService.load(MarketWechatImage.class, wechatImageId);
            marketWechatImageService.update(marketWechatImage.edit(cover));
            result.success().message("修改图片成功");
            
        } catch (Exception e) {
            
            logger.error("onEdit - ", e);
            result.fail().message("修改图片失败");
            
        }
        
        return JsonUtils.toJsonString(result);
    }

}
